package pers.cocoadel.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import pers.cocoadel.springcloud.service.PaymentSerivce;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author ZhenWuWang
 */
@RestController
@RefreshScope
public class PaymentController
{
    @Value("${server.port}")
    private String serverPort;

    @Value("${config.info:null}")
    private String configInfo;

    @Value("${config.ext-info:ext-info-null}")
    private String extConfigInfo;

    @Value("${config.comm-info:null}")
    private String commConfigInfo;

    @Resource
    private PaymentSerivce paymentSerivce;


    @GetMapping("/sayHi")
    @SentinelResource(value = "hello", blockHandler = "blockHandlerEx")
    public String sayHi()
    {
        return "hi!" + (new Date()).getTime();
    }

    public static String blockHandlerEx(BlockException ex)
    {
        return "服务器繁忙！";
    }

    @GetMapping("/payment/get/{id}")
//    @SentinelResource(value = "payment/get/1",blockHandler = "blockHandlerEx")
    public String getPayment(@PathVariable("id") String id)
    {
        return paymentSerivce.getPaymentById(id);
    }

    @GetMapping("/payment/get1/{id}")
    public String getPayment1(@PathVariable("id") String id)
    {
        return paymentSerivce.getPaymentById(id);
    }


    @GetMapping("/payment/getConfig")
    public String getConfigInfo()
    {
        return "serverPort: " + serverPort + "  configInfo: " + configInfo;
    }

    @GetMapping("/payment/getExtConfig")
    public String geExtConfigInfo()
    {
        return "serverPort: " + serverPort + "  extConfigInfo: " + extConfigInfo;
    }

    @GetMapping("/payment/getCommConfig")
    public String geCommConfigInfo()
    {
        return "serverPort: " + serverPort + "  commConfigInfo: " + commConfigInfo;
    }
}
